package com.it.a10threadsafe2;

public class MyRunnable implements Runnable {
    int ticket = 0;
    @Override
    public void run() {
        while (true) {
            if(method()) break;
        }
    }
    public synchronized boolean method(){
        synchronized (MyRunnable.class) {
            if (ticket == 100) {
                return true;
            }else{
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                ticket++;
                System.out.println(Thread.currentThread().getName()+"卖了"+ticket+"张票！！!");
            }
        }
        return false;
    }
}
